using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Castle.ActiveRecord;
using Longtop.Web.WebSite.Models;
using System.IO;
using System.ComponentModel;
using NHibernate.Criterion;
using Longtop.Web.Helpers;
using Longtop.Web.WebSite.Admin.Shared;
namespace Longtop.Web.WebSite.Admin.Controllers {

    public class CasesController : ControllerBase {

        public ActionResult Index([DefaultValue(1)]int id) {
            var model = new Cases.IndexModel();
            model.Lable = Lable.Find(id);
            return View(model);
        }

        public ActionResult List([DefaultValue(1)]int id, [DefaultValue(1)]int pageIndex) {
            var model = new Cases.ListModel();
            model.PagedList = model.PagedFindAll(pageIndex, new[] { Order.Desc("CreateDT") }, Expression.Eq("Lable", new Lable() { LableId = id }));
            model.Lable = Lable.Find(id);
            return View(model);
        }

        public ActionResult Create(int? id) {
            var model = new Case();
            model.Lable = id == null ? new Lable() : Lable.Find(id);
            return View(model.Lable.EditorView, model);
        }

        public ActionResult Delete(int id) {
            Case.Find(id).Delete();
            return Redirect(this.Request.UrlReferrer.AbsoluteUri);
        }

        //
        // POST: /Products/Create
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(FormCollection collection) {
            var model = new Case();
            model.Lable = new Lable() { LableId = int.Parse(collection.Get("LableId")) };
            try {
                // TODO: Add update logic here
                if (TryUpdateModel(model)) {
                    model.CreateDT = DateTime.Now;
                    model.Name = this.User.Identity.Name;

                    if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0) {
                        var filePath = "~/userfiles/products/" + model.CaseId + "/";
                        var oPath = Server.MapPath(filePath);
                        if (!Directory.Exists(oPath)) {
                            Directory.CreateDirectory(oPath);
                        }
                        var fileName = oPath + Path.GetFileName(Request.Files[0].FileName);
                        if (System.IO.File.Exists(fileName)) System.IO.File.Delete(fileName);
                        Request.Files[0].SaveAs(fileName);

                        model.Logo = filePath + Path.GetFileName(fileName);
                    }
                    model.Save();
                    return RedirectToAction("index", new { id = model.Lable.LableId });
                }
                return View(model.Lable.EditorView, model);
            }
            catch (Exception ex) {
                ModelState.AddModelError("result", ex.Message);
                return View(model.Lable.EditorView, model);
            }
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection) {
            var model = Case.Find(id);
            try {
                // TODO: Add update logic here
                if (TryUpdateModel(model)) {
                    model.Lable = new Lable() { LableId = int.Parse(collection.Get("LableId")) };
                    if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0) {
                        var filePath = "~/userfiles/products/" + model.CaseId + "/";
                        var oPath = Server.MapPath(filePath);
                        if (!Directory.Exists(oPath)) {
                            Directory.CreateDirectory(oPath);
                        }
                        var fileName = oPath + Path.GetFileName(Request.Files[0].FileName);
                        if (System.IO.File.Exists(fileName)) System.IO.File.Delete(fileName);
                        Request.Files[0].SaveAs(fileName);

                        model.Logo = filePath + Path.GetFileName(fileName);
                    }
                    model.Save();
                    return RedirectToAction("index", new { id = model.Lable.LableId });
                }
                model.Refresh();
                return View(model.Lable.EditorView ?? "create", model);
            }
            catch (Exception ex) {
                ModelState.AddModelError("result", ex.Message);
                return View(model.Lable.EditorView ?? "create", model);
            }
        }



        //
        // GET: /Products/Edit/5

        public ActionResult Edit(int id) {
            var model = Case.Find(id);
            return View(model.Lable.EditorView ?? "Create", Case.Find(id));
        }


    }
}
